﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Common.ComponentModel;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Catalog;
using MenuItem=System.Web.UI.WebControls.MenuItem;

namespace Shop.Web.Controls
{
    public partial class LeftMenu : System.Web.UI.UserControl
    {
        private ICollection<MenuItem> ds;
        private string cssClass;
        protected string queryType;

        public ICollection<MenuItem> DataSourse
        {
            get { return ds; }
            set { ds = value; }
        }

        public string CssClass
        {
            get { return cssClass; }
            set { cssClass = value; }
        }

        public string QueryType
        {
            get { return queryType; }
            set { queryType = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            IOrderedEnumerable<CatalogMenuItem> items = Locator.GetService<IMenuItemManager>().FindAll().OrderByDescending(item => item.Query);
            Dictionary<string, List<CatalogMenuItem>> dictionary = new Dictionary<string, List<CatalogMenuItem>>();
            foreach (CatalogMenuItem item in items)
            {
                string pair;
                if (item.Query == "catId")
                {
                    pair = StringUtils.IsBlankOrNull(item.Parent) ? item.Name : item.Parent;
                }
                else
                {
                    pair = "Бренды";
                }
                if (!dictionary.Keys.Contains(pair))
                    dictionary.Add(pair, new List<CatalogMenuItem>());
                if (item.Name != pair && !dictionary[pair].Contains(item))
                dictionary[pair].Add(item);
            }
            rprMenu.DataSource = dictionary;
            rprMenu.DataBind();
        }
    }
}